/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.commons;

import java.util.Map;
import javax.swing.JLabel;
import org.balisunrise.common.Message;
import org.balisunrise.common.VisualEntity;
import org.balisunrise.common.VisualEntityType;
import org.balisunrise.common.abstracts.AbstractVisualEntity;
import org.balisunrise.swing.components.panel.MessengerPanel;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;

/**
 *
 * @author Glauco
 */
public class Messenger extends AbstractVisualEntity
        implements org.balisunrise.common.Messenger<MessengerPanel>{

    private MessengerPanel visualRepresentation;

    public void setVisualRepresentation(MessengerPanel visualRepresentation) {
        this.visualRepresentation = visualRepresentation;
        visualRepresentation.buildLayout();
    }

    @Override
    public void setMode(int mode) {
        if(visualRepresentation == null)
            return ;
        visualRepresentation.setMode(mode);
    }

    @Override
    public void addMessage(String id, String message, int level) {
        if(visualRepresentation == null)
            return ;
        visualRepresentation.addMessage(id, message, level);
    }

    @Override
    public void addMessage(Message msg) {
        addMessage((String) msg.getId(),msg.getMessage(), msg.getLevel());
    }

    @Override
    public void notify(String message, int level) {

        int ndLevel = NotifyDescriptor.INFORMATION_MESSAGE;

        switch(level){
            case CONFIRMATION:
                ndLevel = NotifyDescriptor.PLAIN_MESSAGE;
                break;
            case INFORMATION:
                ndLevel = NotifyDescriptor.INFORMATION_MESSAGE;
                break;
            case WARNING:
                ndLevel = NotifyDescriptor.WARNING_MESSAGE;
                break;
            case ERROR:
                ndLevel = NotifyDescriptor.ERROR_MESSAGE;
                break;
        }

        NotifyDescriptor d = new NotifyDescriptor.Message(message,ndLevel);
        DialogDisplayer.getDefault().notify(d);
    }

    @Override
    public void notify(Message msg) {
        notify(msg.getMessage(), msg.getLevel());
    }

    @Override
    public void removeMessage(String id) {
        if(visualRepresentation == null)
            return ;
        visualRepresentation.removeMessage(id);
    }

    @Override
    public void removeAllMessages() {
        if(visualRepresentation == null)
            return ;
        visualRepresentation.removeAllMessages();
    }

    @Override
    public MessengerPanel getVisualRepresentation() {
        return visualRepresentation;
    }

    @Override
    public boolean hasError() {

        if(visualRepresentation == null)
            return false;

        Map<String,JLabel> messages = visualRepresentation.getMessages();

        if(messages.isEmpty())
            return false;

        for (JLabel msg : messages.values()) {
            int level = Integer.parseInt(
                    msg.getAccessibleContext().getAccessibleDescription());
            if(level == 3)
                return true;
        }
        
        return false;
    }

    /**
     * Retorna o tipo do VisualEntity.<BR>
     * Sempre retornará MESSENGER.
     * @return VisualEntityType.MESSENGER
     */
    @Override
    public VisualEntityType getVisualEntityType() {
        return VisualEntityType.MESSENGER;
    }

    @Override
    public void setOwner(VisualEntity owner) {
        super.setOwner(owner);
    }
}
